﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：ProcureCatalogList.cs
    // 文件功能描述：采购目录查询
    // 创建标识：吕海乐2009-05-15
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    public partial class ProcureCatalogList : BasePage
    {
        public ProcureCatalogList()
        {
            this.ModuleNo = Module.采购产品管理.ToString();
            this.PageOperateType = OperateType.新增采购产品.ToString();
        }
        IBLL_Hospital ih = Factory.BLL_Hospital();
        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HZ.DrugTrade.Web.Function.IsThreeLevel())//项目查询条件仅三级医院可见
            {
                this.ddlProjectid.Visible = false;
                this.lblProjectid.Visible = false;
            }
            if (!IsPostBack)
            {
                //绑定所有项目
                //ProcurementProject procurementProject = new ProcurementProject();
                //procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                //procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                //procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                //procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                //int temp;
                //IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                //ddlProject.DataSource = projectList;
                //ddlProject.DataTextField = "ProjectName";
                //ddlProject.DataValueField = "ProjectID";
                //ddlProject.DataBind();
                //ListItem item = new ListItem();
                //item.Text = Constants.ALL_ITEM_TEXT;
                //item.Value = Constants.ALL_ITEM_VALUE;
                //ddlProject.Items.Insert(0, item);
                setBind();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            //setBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 根据查询条件绑定采购目录
        /// </summary>
        private void setBind()
        {
            IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            procureCatalog.PackUnit = this.txtPackUnit.Text.Trim().ToUpper();
            procureCatalog.Outlookc = HZ.Fun.CheckString(txtOutLook.Text.Trim());
            //procureCatalog.Pack = txtPack.Text.Trim();
            if (this.ddlProjectid.Visible == true)
            {
                procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            }
            else
            {
                procureCatalog.ProjectID = "0001";
            }
            procureCatalog.GoodsID = txtGoodsId.Text.Trim();
            procureCatalog.PinyinCode = this.txtpinyincode.Text.Trim();
            //procureCatalog.ProductName = txtProductName.Text.Trim();
            procureCatalog.Unit = txtUnit.Text.Trim();
            procureCatalog.IsUsing = true;
            procureCatalog.Material = HZ.Fun.CheckString(txtMaterial.Text.Trim());
            procureCatalog.QualityLevel = this.txtQualityLevel.Text;
            //procureCatalog.SortName = txtProductType.Text;
            procureCatalog.GoodsName = HZ.Fun.CheckString(txtGoodsName.Text);
            procureCatalog.CompanyName_PS = txtCompanyname_PS.Text.Trim();
            procureCatalog.SortFields = " GoodsName asc,ProcureCatalogID";
            //string orgID = CurrentAccount.GetUser().Org.OrgID;
            //bool flag = ipb.GetIsJiCeng(orgID);//判断是否基层医疗机构 add by zdc
            //if (flag)//盟市添加的只能采购基本药物和内蒙基本药物 zdc
            //{
            //    procureCatalog.IsJiCeng = 0;
            //}
            int totalCount = 0;

            try
            {

                IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere_Hospital(procureCatalog, CurrentAccount.GetUser().UserID, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                AspNetPager1.RecordCount = totalCount;
                gvwProduceCatalog.DataSource = procureCatalogList;
                gvwProduceCatalog.DataBind();
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }
        }

        /// <summary>
        /// 简单查询与高级查询间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtGoodsId.Text = "";
                txtMaterial.Text = "";
                txtOutLook.Text = "";
                txtPackUnit.Text = "";
                txtQualityLevel.Text = "";
                txtProduceCompany_SC.Text = "";
                // txtProductType.Text = "";
                txtUnit.Text = "";
                txtGoodsName.Text = "";
                txtProduceCompany_SC.Text = "";
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            procureCatalog.CompanyName_PS = txtCompanyname_PS.Text.Trim();
            procureCatalog.PackUnit = this.txtPackUnit.Text.Trim().ToUpper();
            procureCatalog.Outlookc = HZ.Fun.CheckString(txtOutLook.Text.Trim());
            procureCatalog.GoodsID = txtGoodsId.Text.Trim();
            procureCatalog.Unit = txtUnit.Text.Trim();
            procureCatalog.PinyinCode = this.txtpinyincode.Text.Trim();
            if (this.ddlProjectid.Visible != false)
            {
                procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            }
            else
            {
                procureCatalog.ProjectID = "0001";
            }
            procureCatalog.IsUsing = true;
            procureCatalog.Material = HZ.Fun.CheckString(txtMaterial.Text.Trim());
            procureCatalog.QualityLevel = this.txtQualityLevel.Text;
            procureCatalog.GoodsName = HZ.Fun.CheckString(txtGoodsName.Text);
            procureCatalog.SortFields = " GoodsName asc,Goodsid";
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.ProcureCatalog>(procureCatalog, true);
            string sql = string.Format(@"select Goodsid 产品编号,GoodsName 产品名称,Outlookc 规格型号,Material 材质,Unit 单位,
                                        PackUnit 包装单位,QualityLevel 产品类别,CompanyName_SC 生产企业,ProcurePrice 采购限价,price 实际采购价格 from (SELECT a.Goodsid ,a.GoodsName ,a.Outlookc ,a.Material ,a.Unit ,
                                        a.PackUnit ,a.QualityLevel ,a.CompanyName_SC ,a.ProcurePrice ,b.price ,a.IsUsing,a.projectid,a.PinyinCode
                                          FROM View_ProcureCatalog_tb a left join (select * from tb_hospitalprocurecatalog where hospitalid = '{1}') b
                                          on a.procurecatalogid = b.procurecatalogid)  WHERE 1=1 {0}", where, CurrentAccount.GetUser().UserID);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "基础产品库.xls");
        }

        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (ddlProjectid.SelectedItem.Value == "0002")
                {
                    e.Row.Cells[10].Text = "自主报价";
                }
                else
                {
                    e.Row.Cells[10].Text = "采购限价";
                }
            }
        }
    }
}
